using System.Collections.Generic;
using UnityEngine;


public struct ScrollViews
{
    public string name;
    public float width;
    public GameObject middle;
    public GameObject left;
    public GameObject right;

}

public class ScrollScene : MonoBehaviour
{

    List<ScrollViews> m_ScrollViews = new List<ScrollViews>();
    void Start()
    {
        var landscapesTr = transform.Find("Landscapes");
        if (!landscapesTr)
            return;

        for (var i = 0; i < landscapesTr.childCount; ++i)
        {
            var child = landscapesTr.GetChild(i);
            var childMiddle = child.Find("Middle");
            if (!childMiddle)
                continue;
            var view = new ScrollViews()
            {
                name = "",
                width = 100,
                middle = childMiddle.gameObject
            };

            float minX = 100000;
            float maxX = -100000;

            for (var j = 0; j < childMiddle.childCount; ++j)
            {
                var viewObj = childMiddle.GetChild(j);
                var spriteRenderer = viewObj.GetComponent<SpriteRenderer>();
                if (spriteRenderer)
                {
                    var leftSide = spriteRenderer.transform.position.x - spriteRenderer.sprite.rect.width / 2f / spriteRenderer.sprite.pixelsPerUnit;
                    var rightSide = spriteRenderer.transform.position.x + spriteRenderer.sprite.rect.width / 2f / spriteRenderer.sprite.pixelsPerUnit;
                    if (minX > leftSide)
                        minX = leftSide;
                    if (maxX < rightSide)
                        maxX = rightSide;
                }
            }
            view.width = maxX - minX;
            view.left = GameObject.Instantiate(view.middle, view.middle.transform.position + new Vector3(-view.width, 0, 0), transform.rotation);
            view.right = GameObject.Instantiate(view.middle, view.middle.transform.position + new Vector3(+view.width, 0, 0), transform.rotation);
            view.left.transform.SetParent(view.middle.transform.parent, true);
            view.right.transform.SetParent(view.middle.transform.parent, true);
            m_ScrollViews.Add(view);
        }
    }

    // Update is called once per frame
    void Update()
    {
        UpdateView();
    }

    void UpdateView()
    {
        var cameraPos = Camera.main.transform.position;
        for (var i = 0; i < m_ScrollViews.Count; ++i)
        {
            var view = m_ScrollViews[i];
            var distance = cameraPos.x - view.middle.transform.position.x;
            if (distance > view.width / 2 + 0.2)
            {
                view.left.transform.position = view.right.transform.position + new Vector3(view.width, 0, 0);
                var t = view.right;
                view.right = view.left;
                view.left = view.middle;
                view.middle = t;
            }
            else if (distance < -(view.width / 2 + 0.2))
            {
                view.right.transform.position = view.left.transform.position - new Vector3(view.width, 0, 0);
                var t = view.left;
                view.left = view.right;
                view.right = view.middle;
                view.middle = t;
            }
            m_ScrollViews[i] = view;
        }
    }
}
